feat(llm): add Anthropic (Claude) client helper behind [llm] extra#28
Conversation
WalkthroughA new ChangesAnthropic (Claude) LLM Helper
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsErrors were encountered while retrieving linked issues. Errors (1)
Comment |
4850003 to
b038307
Compare
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@docs/guides/llm-integration.md`:
- Around line 130-136: The failure list for call_tool is too narrow and
underdescribes when None is returned. Update the documentation around call_tool
to say it warns and returns None for any exception raised by
client.messages.create, along with the existing missing tool_use, non-object
input, and missing tool name cases. Use the call_tool description in
llm-integration.md to locate and broaden the wording.
- Around line 83-107: The description of `call_tool` overstates its behavior by
claiming it guarantees schema validation and retries on mismatches; update the
docs in the `call_tool` / `tool_choice` example to say it only forces the named
tool and returns `tool_use.input`. Keep the `strict: True` note for the tool
schema, but clarify that the caller must still validate or coerce the returned
result rather than relying on retry guarantees.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 19a2596c-a9f0-4e9e-8cf3-d14c43d241a1
📒 Files selected for processing (11)
AGENTS.mdREADME.mddocs/README.mddocs/guides/llm-integration.mdnui_shared_utils/__init__.pynui_shared_utils/llm.pypyproject.tomlrequirements-test.txtsetup.pytests/test_lazy_imports.pytests/test_llm.py
Three Lambda repos hand-rolled the same Anthropic client + forced-tool-use
call + tool_use-block parse; this adds one tested helper (nui_shared_utils.llm)
so they share it instead of diverging. Plumbing only: prompts, tool schemas,
and model ids stay in consumers.
- build_anthropic_client handles both auth modes: API-key (explicit ->
ANTHROPIC_API_KEY env -> Secrets Manager) and Bedrock IAM.
- call_tool makes a forced tool-use call and returns the named tool's input
dict, best-effort (None on any model/parse failure, never raises);
call_text returns {text, input_tokens, output_tokens}.
- Ships behind a new optional [llm] extra (anthropic[bedrock]>=0.45.0,<1.0.0),
kept out of `all`. Lazy-loaded like the other optional integrations:
`import nui_shared_utils` does not import anthropic (cold-start preserved),
and the top-level exports resolve to None when the extra is not installed.
- Docs: docs/guides/llm-integration.md, README usage example, AGENTS.md.
b038307 to
49e604c
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@docs/guides/llm-integration.md`:
- Around line 83-88: The description of `strict` in the LLM integration guide is
inconsistent with the example tool shape, which can mislead readers about where
to place it. Update the wording around the tool definition to state that
`strict` is a top-level field on the tool object, not part of `input_schema`,
and ensure the explanation matches the example shown in the guide so the
`tool_choice`/tool schema usage is described consistently.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 57ae501f-68c9-40cc-966f-30c09adc45f9
📒 Files selected for processing (11)
AGENTS.mdREADME.mddocs/README.mddocs/guides/llm-integration.mdnui_shared_utils/__init__.pynui_shared_utils/llm.pypyproject.tomlrequirements-test.txtsetup.pytests/test_lazy_imports.pytests/test_llm.py
✅ Files skipped from review due to trivial changes (4)
- docs/README.md
- AGENTS.md
- setup.py
- README.md
🚧 Files skipped from review as they are similar to previous changes (5)
- pyproject.toml
- requirements-test.txt
- tests/test_lazy_imports.py
- nui_shared_utils/llm.py
- nui_shared_utils/init.py
Summary
Several Lambda functions independently hand-rolled the same Anthropic client + forced-tool-use call +
tool_use-block parse. This adds one tested helper,nui_shared_utils.llm, so they can share it instead of diverging. Plumbing only: prompts, tool schemas, and model ids stay in the caller.build_anthropic_client(mode="api_key"|"bedrock", ...)handles both auth modes: API-key (explicit ->ANTHROPIC_API_KEYenv -> AWS Secrets Manager) and Bedrock IAM.call_tool(client, *, tool, prompt, model, max_tokens, system=None)makes a forced tool-use call and returns the named tool'sinputdict, best-effort:Noneon any model/parse failure, never raises.call_text(client, *, prompt, model, max_tokens, system=None)returns{text, input_tokens, output_tokens}; transport errors propagate.[llm]extra (anthropic[bedrock]>=0.45.0,<1.0.0), kept out ofall. Lazy-loaded like the other optional integrations, soimport nui_shared_utilsdoes not importanthropic(cold-start preserved) and the top-level exports resolve toNonewhen the extra is not installed.Backwards compatibility
Purely additive. New module, new optional extra, new lazy exports. No existing public API, signature, or behaviour changes. Installing without
[llm]is unaffected (anthropicis not a base dependency).Changes
nui_shared_utils/llm.py(new),nui_shared_utils/__init__.py(lazy exports + optional submodule)tests/test_llm.py(new, 21 tests),tests/test_lazy_imports.py(cold-start regression)setup.py,pyproject.toml([llm]extra, identical in both),requirements-test.txt(test dependency)README.md,docs/README.md,docs/guides/llm-integration.md(new),AGENTS.mdTest plan
call_toolhappy path + allNonepaths,call_textusage extraction)import nui_shared_utilsdoes not loadanthropic; missing-extra resolves lazy exports toNoneblack --checkandmypyclean onllm.py;python -m build+twine checkpassanthropicis not a base dependency (pip install -e .does not pull it)🤖 Generated with Claude Code
Summary by CodeRabbit
call_textandcall_toolutilities.[llm]install extra supporting both API-key and AWS Bedrock IAM auth.